package hot;

/**
 * @author way
 * @create 2025-03-13 21:45
 */
public class _124_maxPathSum {
    int ans = Integer.MIN_VALUE;

    public int maxPathSum(TreeNode root) {
        if (root == null) return 0;

        dfs(root);
        return ans;
    }

    //返回最大链和
    private int dfs(TreeNode root) {
        if (root == null) return 0;

        int lChainMax = dfs(root.left);
        int rChainMax = dfs(root.right);
        ans = Math.max(lChainMax + rChainMax + root.val, ans);
        return Math.max(Math.max(lChainMax, rChainMax) + root.val, 0);
    }
}
